package c2.cross.business.ladder.rank;

import c2.cross.business.CrossBody;
import c2.cross.business.ladder.common.LadderOppoDetail;
import cate.common.table.d.GDLadder;
import cate.common.table.pvp.ladder.LadderRefreshRow;
import cate.game.pvp.ladder.common.LadderOppoSnapshot;
import cate.game.pvp.ladder.common.LadderRefreshQuery;
import cate.game.pvp.ladder.common.LadderRefreshResult;
import lombok.extern.slf4j.Slf4j;

import java.util.*;

@Slf4j
public class LadderRefreshDo {

    public LadderRefreshResult exe(CrossBody context, LadderRankModule rankMod, LadderRefreshQuery query) {
        LadderRefreshResult result = new LadderRefreshResult();
        result.recentRefreshTime = context.time.now();
        List<LadderOppoDetail> theirDetails = pickOppoForRole(context, rankMod, query);
        List<LadderOppoSnapshot> theirSnaps = new ArrayList<>();
        for(LadderOppoDetail detail : theirDetails) {
            LadderOppoSnapshot oppoSnapshot = detail.toSnapshot();
            oppoSnapshot.avatarTid = detail.rolePs.snapshot.base.avatar.tid;
            oppoSnapshot.srvId = detail.rolePs.snapshot.base.srvId;
            theirSnaps.add(oppoSnapshot);
        }
        result.opponents = theirSnaps;
        result.success();
        return result;
    }

    /** 根据规则为玩家挑选出适合他的对手 */
    private List<LadderOppoDetail> pickOppoForRole(CrossBody context, LadderRankModule rankMod, LadderRefreshQuery query) {
        List<LadderOppoDetail> they = new ArrayList<>(query.num);
        List<LadderOppoDetail> rankList = rankMod.getList();//完整的排行榜
        int itsRankNo = rankMod.getRankNo(query.roleId);
        Random random = new Random();
        LadderRefreshRow row;
        if(itsRankNo <= GDLadder.RANK_NO_NOT_IN_LIST) {
            // 未上榜
            itsRankNo = rankList.size() + 1;
            row = context.table.ladder.refresh.findRow(rankList.size());
        } else {
            // 已上榜
            row = context.table.ladder.refresh.findRow(itsRankNo);
        }
        // 玩家刷新时只可以刷新到比自己排名高的玩家
        for (int i = 1; i < row.limit+1; i++) {
            int range = random.nextInt(row.step);
            int rank = itsRankNo - (row.step * i) + range;
            if (rank <= 0) {
                //高排名玩家不够了，选取低排名玩家，同样按照衰减的范围
                rank = itsRankNo + (row.step * (row.limit-i+1)) - range;
            }
            they.add(rankList.get(rank - 1));
        }
        //原方案500-100之间的玩家刷新时只可以刷新到比自己排名高的玩家
        //100之后的玩家刷新时固定会出现两名排名比自己低的玩家
        /*if (itsRankNo > 100) {
            for (int i = 1; i < row.limit+1; i++) {
                int range = random.nextInt(row.step);
                int rank = itsRankNo - (row.step * i) + range;
                they.add(rankList.get(rank - 1));
            }
        } else {
            for (int i = 1; i < row.limit-1; i++) {
                int range = random.nextInt(row.step);
                int rank = itsRankNo - (row.step * i) + range;
                they.add(rankList.get(rank - 1));
            }
            for (int i = 1; i < 3; i++) {
                int range = random.nextInt(row.step);
                int rank = itsRankNo + (row.step * i) - range;
                they.add(rankList.get(rank - 1));
            }
        }*/
        return they;
    }

}
